<?php

namespace App\Admin\Extensions;

use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;

class ChangeStatus extends RowAction
{

    protected $model;

    protected $placeholer;

    public function __construct(string $model = null, string $placeholer = '状态切换')
    {
        $this->model = $model;
        $this->placeholer = $placeholer;
    }

    public function title()
    {
        return "<i title='{$this->placeholer}' class='feather icon-toggle-left
                grid-action-icon' style='margin-right: 8px'></i>";
    }

    public function handle(Request $request)
    {
        $id = $this->getKey();

        $status = $request->get('status');

        $model = $request->get('model');

        if ($status == 1) {
            $changed_status = 0;
        } else {
            $changed_status = 1;
        }

        $result = $model::find($id);

        $result -> status = $changed_status;
        $result -> save();

        return $this->response()->success('操作成功')->refresh();
    }

    public function parameters ()
    {
        return [
            'status' => $this->row->status,
            'model' => $this->model,
        ];
    }

}
